#define _CRT_SECURE_NO_WARNINGS
#include"test.h"
void SLinit(SL* p)
{
	assert(p);
	p->arr = NULL;
	p->kong = p->size = 0;
}
void SLget(SL* p)
{
	assert(p);
	free(p->arr);
	p->arr = NULL;
	p->kong = p->size;
}
void swap(leixing* e1, leixing* e2)
{
	leixing time = *e1;
	*e1 = *e2;
	*e2 = time;
}
void SLdui(leixing* p, size_t zi)
{
	int fu = (zi - 1)/2;
	while (zi>0)
	{
		if (p[fu] > p[zi])
		{
			swap(&p[fu], &p[zi]);
			zi = fu;
			fu = (zi - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
void SLcha(SL* p, leixing x)
{
	assert(p);
	if (p->kong == p->size)
	{
		int time = p->kong == 0 ? 4 : 2 * p->kong;
		leixing* ass = (leixing*)realloc(p->arr, sizeof(leixing) * time);
		if (ass == NULL)
		{
			perror("realloc ass");
			return;
		}
		p->arr = ass;
		p->kong = time;
	}
	p->arr[p->size++] = x;
	SLdui(p->arr, p->size - 1);
}
void SLxiafa(leixing* p, int yuan, int wei)
{
	int xia = wei * 2 + 1;
	while (xia<yuan)
	{
		if ((xia+1)<yuan&&p[xia] > p[xia + 1])
		{
			xia++;
		}
		if (p[xia] < p[wei])
		{
			swap(&p[xia], &p[wei]);
			wei = xia;
			xia = wei * 2 + 1;
		}
		else
		{
			break;
		}
	}	
}
void SLshan(SL* p)
{
	assert(p&&p->size>0);
	swap(&p->arr[0], &p->arr[p->size - 1]);
	p->size--;
	SLxiafa(p->arr, p->size, 0);
}
leixing SLkan(SL* p)
{
	assert(p && p->size > 0);
	return p->arr[0];
}
bool SLkong(SL* p)
{
	assert(p);
	return p->size == 0;
}